<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>1_引出生命周期</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>
    <!-- 引入React -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/babel">
        // 创建组件
        // 生命周期回调函数 == 生命周期钩子函数 == 生命周期函数 == 生命周期钩子
        class Left extends React.Component {
            state = { opacity: 1 }
            death = () => {
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById("test"));
            }
            componentDidMount() {
                console.log("组件挂载完成");
                this.timer = setInterval(() => {
                    let { opacity } = this.state;
                    opacity -= 0.1;
                    if (opacity <= 0) opacity = 1;
                    this.setState({ opacity });
                }, 200);
            }
            componentWillUnmount() {
                console.log("组件即将卸载");
                // 清除定时器
                clearInterval(this.timer);
            }
            // 初始化渲染，状态更新之后
            render() {
                return (
                    <div>
                        <h1 style={{ opacity: this.state.opacity }}>学不会react，怎么办？</h1>
                        <button onClick={this.death}>不活了</button>
                    </div>
                )
            }
        }
        // 渲染组件到页面
        ReactDOM.render(<Left />, document.getElementById("test"));
    </script>
</body>

</html>